package com.itcast.tanhua.dubbo.server.api;


import com.alibaba.dubbo.config.annotation.Service;


import com.itcast.tanhua.dubbo.server.domain.RecommendUser;
import com.itcast.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @PachageName: com.itcast.tanhuadubboservice.api
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/1521:49
 * @Version 1.0
 */
@Service(version = "1.0.0")
@Component
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long userId) {
        Query query = Query.query(Criteria.where("toUserId").is(userId)).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return this.mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public PageInfo<RecommendUser> queryPageInfo(Long userId, Integer pageNum, Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Order.desc("score")));

        Query query = Query.query(Criteria.where("toUserId").is(userId)).with(pageRequest);

        List<RecommendUser> list = this.mongoTemplate.find(query, RecommendUser.class);

        return new PageInfo<>(0, pageNum, pageNum, list);
    }

    @Override
    public double queryScore(Long userId, Long toUserId) {

        Query query = Query.query(Criteria.where("userId").is(userId).and("toUserId").is(toUserId));
        RecommendUser one = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (null == one){
            return 0;
        }
        return one.getScore();
    }
}
